<template>
    <div>
        <!-- 商品列表区域 -->
        <div class="goods-list">
            <router-link tag="div" 
            class="goods-item" 
            v-for="(item,index) in goodsList" 
            :key="index"
            :to="'/home/goodslist/'+item.id" >
                <img :src="item.img_url" alt="">
                <h1 class="title">{{item.title}}</h1>
                <div class="info">
                    <p class="price">
                        <span class="new">￥{{item.sell_price}}</span>
                        <del class="old">￥{{item.market_price}}</del>
                    </p>
                    <p class="sell">
                        <span>热卖中</span>
                        <span>还剩{{item.stock_quantity}}件</span>
                    </p>
                </div>
            </router-link>
            
        <!-- 加载更多按钮 -->
        <mt-button type="danger" size="large" @click="loadMore" style="margin-top:10px">加载更多</mt-button>
        </div>
    </div>
</template>

<script>
import {Toast} from 'mint-ui'

export default {
    data:function(){
        return {
            pageindex:1,
            goodsList:[],
            // 节流阀，默认没有加载完数据，等接收到的数据为空时，置为true，再点“加载更多”按钮，就不再发送请求
            loaded:false,
        }
    },
    created:function(){
        this.getGoodsList();
    },
    methods:{
        getGoodsList:async function () {
            const {data} = await this.$http('/api/getgoods?'+this.pageindex);
            console.log(data);
            if(data.status===0) {
                if(!data.message.length){
                    this.loaded=true;
                    return Toast({
                        message: '没有更多商品了',
                        position: 'center',
                        duration: 2000
                    });
                }
                this.goodsList=this.goodsList.concat(data.message);
            }
        },
        // 加载更多
        loadMore:function () {  
            if(this.loaded) return;
            this.pageindex++;
            this.getGoodsList();
        },
    },
}
</script>

<style lang="scss" scoped>
.goods-list{
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    padding:7px;
    padding-top:0;
    .goods-item{
        width:49%;
        border: 1px solid rgb(211, 211, 211);
        box-shadow: 0 0 5px rgb(173, 173, 173);
        margin-top: 7px;
        padding: 2px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        img{
            width: 100%;
            min-height: 170px;
        }
        .title{
            font-size: 16px;
            padding: 5px;
        }
        .info{
            background-color: rgb(221, 220, 220);
            padding: 5px;

            .price{
                .new{
                    font-weight: bold;
                    color: red;
                    font-size: 14px;
                    margin-right: 10px;
                }
                .old{
                    font-size: 12px;
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
                margin-bottom: 0;
            }
        }
    }
}
</style>